package clone;

import java.util.ArrayList;
import java.util.List;
/**
 * 基本数据类型可以实际clone过去
 * 引用类型只是clone一个引用
 *
 */
public class CloneDemo implements Cloneable{
	private String s;
	private int i;
	private ArrayList<String> list=new ArrayList<String>();
	public String getS() {
		return s;
	}

	public ArrayList<String> getList() {
		return list;
	}

	public void setList(ArrayList<String> list) {
		this.list = list;
	}



	public void setS(String s) {
		this.s = s;
	}

	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

	@Override
	protected CloneDemo clone() throws CloneNotSupportedException {
		CloneDemo cloneDemo=null;
		cloneDemo=(CloneDemo) super.clone();
		//实现引用类型的深拷贝
		cloneDemo.list=(ArrayList<String>) this.list.clone();
		return cloneDemo;
	}

	public static void main(String[] args) throws CloneNotSupportedException, InstantiationException, IllegalAccessException {
		CloneDemo cloneDemo=new CloneDemo();
		cloneDemo.setI(1);
		cloneDemo.setS("g");
		cloneDemo.getList().add("a");
		CloneDemo cloneDemo1=cloneDemo.clone();
		cloneDemo1.getList().add(0, "b");
		System.out.println(cloneDemo1.getI()+cloneDemo1.getS()+cloneDemo1.getList().get(0));
		System.out.println(cloneDemo.getList().get(0));
		System.out.println(cloneDemo.getClass().newInstance().hashCode());
		System.out.println(cloneDemo1.getClass().newInstance().hashCode());

	}

}
